﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Windows;
using System.Collections;

namespace CSharp.Libs.Koc
{
    public class WriteDB
    {
        private Kernel _KRNL = null;
        private DatabaseOperations _DBOps = null;

        public WriteDB(
            Kernel KRNL)
        {
            _KRNL = KRNL;

            //Initialize db
            _DBOps = new DatabaseOperations(_KRNL);
            _DBOps.databaseFileMessage += new DatabaseOperations.DatabaseFileMessageEventHandler(_DBOps_databaseFileMessage);

            _DBOps.InitDb(
                Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\KocInformant.s3db",
                _getDbSqlString());

            //testwise reduce time difference to a minimum
            _DBOps.KocInformant_PrevtickMinDifference = 1;
        }
        ~WriteDB()
        {
            _DBOps.databaseFileMessage -= _DBOps_databaseFileMessage;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void _DBOps_databaseFileMessage(object sender, DatabaseFileMessageEventArgs e)
        {
            MessageBox.Show(e.Message);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// <history>created  19.08.2012</history>
        private string _getDbSqlString()
        {
            return String.Concat(
                "CREATE TABLE [tblCities] (",
                "[ID] INTEGER DEFAULT '1' NOT NULL PRIMARY KEY AUTOINCREMENT,",
                "[InformationID] NUMERIC  NOT NULL,",
                "[CityId] NUMERIC  NULL,",
                "[Name] NVARCHAR(64)  NULL,",
                "[XCoord] NUMERIC  NULL,",
                "[YCoord] NUMERIC  NULL,",
                "[Tax] NUMERIC  NULL,",
                "[Gold] NUMERIC  NULL,",
                "[Food] NUMERIC  NULL,",
                "[Wood] NUMERIC  NULL,",
                "[Stone] NUMERIC  NULL,",
                "[Ore] NUMERIC  NULL,",
                "[Aetherstone] NUMERIC  NULL",
                ");",
                Environment.NewLine,
                "CREATE TABLE [tblInformation] (",
                "[ID] INTEGER DEFAULT '1' NOT NULL PRIMARY KEY AUTOINCREMENT,",
                "[UserId] NUMERIC  NOT NULL,",
                "[FbUId] NUMERIC  NULL,",
                "[NameInGame] NVARCHAR(64)  NULL,",
                "[ForeName] NVARCHAR(64)  NULL,",
                "[LastName] NVARCHAR(64)  NULL,",
                "[ServerId] NUMERIC  NULL,",
                "[PrevTick] NUMERIC  NULL,",
                "[SumGold] NUMERIC  NULL,",
                "[SumFood] NUMERIC  NULL,",
                "[SumWood] NUMERIC  NULL,",
                "[SumStone] NUMERIC  NULL,",
                "[SumOre] NUMERIC  NULL,",
                "[SumAetherstone] NUMERIC  NULL",
                ");",
                Environment.NewLine,
                "CREATE INDEX [idxCities] ON [tblCities](",
                "[InformationID]  ASC,",
                "[CityId]  ASC,",
                "[Name]  ASC",
                ");",
                Environment.NewLine,
                "CREATE INDEX [idxInformation] ON [tblInformation](",
                "[UserId]  ASC,",
                "[FbUId]  ASC,",
                "[NameInGame]  ASC,",
                "[ServerId]  ASC,",
                "[PrevTick]  ASC",
                ");");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PI"></param>
        /// <history>created  19.08.2012</history>
        public Boolean WritePlayerItem(
            PlayerItem PI)
        {
            //_DBOps.KocInformant_GetUserServerCounts();

            //Write PlayerItem
            if (!_DBOps.KocInformant_SavePlayerItem(PI))
            {
                //Something went wrong. :(
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>returns the count of each user / server combination off database
        /// </summary>
        /// <returns></returns>
        /// <history>created  04.09.2012</history>
        public ArrayList GetUserServerCounts()
        {
            return _DBOps.KocInformant_GetUserServerCounts();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PI"></param>
        /// <returns></returns>
        /// <history>created  06.09.2012</history>
        public InfoItemBasic GetUserServerCountByPlayerItem(
            PlayerItem PI)
        {
            return _DBOps.KocInformant_GetUserServerCountByUserServer(PI.UserId.ToString(), PI.ServerId.ToString());
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="ServerId"></param>
        /// <returns></returns>
        /// <history>created  11.09.2012</history>
        public List<TblInformationItem> GetInformationsByUserServer(
            Int64 UserId,
            Int64 ServerId)
        {
            return _DBOps.KocInformant_GetInformationsByUserServer(UserId, ServerId);
        }
    }
}
